import { Component, OnInit } from '@angular/core';
import {UserService} from "../../user/user.service";
import {Response} from "@angular/http";
import {GlobalService} from "../../global.service";

@Component({
  selector: 'app-home-manager',
  templateUrl: './home-manager.component.html',
  styleUrls: ['./home-manager.component.css']
})
export class HomeManagerComponent implements OnInit {

  imgCommon:string = '../../assets/images/';
  imgUrl:any;
  api:any;

  constructor(
    private userService:UserService,
    private globalService:GlobalService,
  ) { }

  ngOnInit() {
    this.api = this.globalService.api;
    this.loadImageInfos();
    this.loadFileInfos();
  }

  imageInfos:any;
  loadImageInfos(){
    let homeResourceInfo = {
      properties:{type:0,status:1}
    }
    this.userService.getImageList(homeResourceInfo)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          console.log("data:",data);
          this.imageInfos = data.value;
        }
      });
  }
  fileInfos:any;
  loadFileInfos(){
    let homeResourceInfo = {
      properties:{type:1,status:1}
    }
    this.userService.getImageList(homeResourceInfo)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          console.log("data:",data);
          this.fileInfos = data.value;
        }
      });
  }

  onImgUpload(event) {
    let response = JSON.parse(event.xhr.response);
    console.log(response);
    if(response.success){
      this.imgUrl = this.imgCommon + response.value.split("@")[2];
      console.log("imgUrl:",this.imgUrl);
      this.loadImageInfos();
    }else{
      alert(response.msg);
    }
  }

  onFileUpload(event) {
    let response = JSON.parse(event.xhr.response);
    console.log(response);
    if(response.success){
      let fileName = response.value.split("@")[2];
      console.log("fileName:",fileName);
      this.loadFileInfos();
    }else{
      alert(response.msg);
    }
  }
  showUpdateFlag:boolean = false;
  showUpdate(data){
    console.log("data:",data);
    this.showUpdateFlag = true;

    this.code = data.code;
    this.imgLinkUrl = data.url;
    this.imgIntroduce = data.introduce;
  }
  code:any;
  imgLinkUrl:any;
  imgIntroduce:any;
  updateImgInfo(){
    let homeResource = {
      code:this.code,
      url:this.imgLinkUrl,
      introduce:this.imgIntroduce,
      properties:{update:true},
    }
    this.userService.updateHomeResource(homeResource)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          this.showUpdateFlag = false;
          this.loadImageInfos();
        }
      })

  }

  deleteImg(id){
    let homeResource = {
      code:id,
      properties:{delete:true},
    }
    this.userService.deleteHomeResource(homeResource)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          this.loadImageInfos();
        }
      })
  }
  deleteFile(id){
    let homeResource = {
      code:id,
      properties:{delete:true},
    }
    this.userService.deleteHomeResource(homeResource)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          this.loadFileInfos();
        }
      })
  }

}
